<?php

namespace App\Http\Controllers;

use App\Comment;
use App\Discuss;
use App\Http\Requests;
use App\Message;
use App\User;
use App\Utils;
use App\Permission;
use App\Editlog;
use Auth;
use Cache;
use Cookie;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Input;
use Log;
use Mail;
use Redirect;
use Reponse;
use View;

class disController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function ask(Request $request, $tag = "all")
    {
        $data = DB::select("select id,title from tasks order by id asc");
        return View::make("dis.quiz")->with(compact("data"))->with(compact('tag'));
    }

    public function editq(Request $request, $id)
    {
        $id = Utils::url2id($id);
        $dis = Discuss::find($id);
        $data = DB::select("select id,title from tasks order by id asc");
        return View::make("dis.editq")->with(compact("data"))->with(compact("dis"));
    }


    public function up(Request $request, $id)
    {

        $id = Utils::url2id($id);
        $com = Comment::find($id);
        if (!Permission::user_is_super(Auth::user()))
            return Redirect::to("/q/" . Utils::id2url($com->taskid));
        $dis = Discuss::find($com->taskid);
        $com->jinghua = 1;
        $com->save();
        $u = User::find($com->userid);
        $u->msgcnt += 1;
        $u->save();
        $data = array();
        $data['senderid'] = 1;
        $data['receiverid'] = $com->userid;
        $data['content'] = "你在『" . $dis->title . "』中的回答对提问者以及其他coder产生了实质性的帮助，被设置为精华，同时奖励150跬步。<a href='/q/" . Utils::id2url($dis->id) . "/a/" . Utils::id2url($com->id) . "' target='_blank'>点击查看</a>";
        Message::create($data);

        return Redirect::to("/q/" . Utils::id2url($com->taskid));
    }

    public function deld(Request $request, $id)
    {
        $id = Utils::url2id($id);
        $dis = Discuss::find($id);
        if ($dis->userid != Auth::user()->id && !Permission::user_is_super(Auth::user()))
            return Redirect::to("/discuss/all");
        $dis->delete = 1;
        $dis->save();
        return Redirect::to("/discuss/all");

    }

    public function del(Request $request, $id)
    {
        $id = Utils::url2id($id);
        $com = Comment::find($id);
        if ($com->type == 3) {
            $taskid = Comment::find($com->taskid)->taskid;
        } else {
            $taskid = $com->taskid;
        }
        if ($com->userid != Auth::user()->id && !Permission::user_is_super(Auth::user()))
            return Redirect::to("/q/" . Utils::id2url($com->taskid));
        $dis = Discuss::find($com->taskid);
        $com->delete = 1;
        $com->save();
        if (Permission::user_is_super(Auth::user()) && $com->type != 3) {
            $reason = $request->get('reason');
            if (strlen($reason) == 0)
                $reason = "回答对千里码社区没有帮助或者违反了社区规则";
            $u = User::find($com->userid);
            $u->msgcnt += 1;
            $u->save();
            $data = array();
            $data['senderid'] = 1;
            $data['receiverid'] = $com->userid;
            $data['content'] = "你在『" . $dis->title . "』中的回答已被隐藏，原因为：" . $reason . "。<a href='/q/" . Utils::id2url($dis->id) . "/a/" . Utils::id2url($com->id) . "' target='_blank'>点击查看</a>";
            Message::create($data);

        }
        return Redirect::to("/q/" . Utils::id2url($taskid));
    }

    public function edit(Request $request, $id)
    {
        $id = Utils::url2id($id);
        $com = Comment::find($id);
        if ($com->userid != Auth::user()->id && !Permission::user_is_super(Auth::user()) && false)
            return Redirect::to("/q/" . Utils::id2url($com->taskid));
        $dis = Discuss::find($com->taskid);
        if ($request->isMethod('post')) {
            $input = $request->all();
            $com->comment = $input["text"];
            $com->comment_html = $input["text_html"];
            $com->save();
            $dis->rtime = time();
            $dis->save();

            $editlog = array();
            $editlog['userid'] = Auth::user()->id;
            $editlog['commentid'] = $id;
            $editlog['text'] = $input["text"];
            $editlog['created_at'] = date("Y-m-d H:i:s");
            Editlog::create($editlog);
            if (Auth::user()->id != $com->userid) {
                $u = User::find($com->userid);
                $u->msgcnt += 1;
                $u->save();
                $data = array();
                $data['senderid'] = 1;
                $data['receiverid'] = $com->userid;
                $data['content'] = "你在『" . $dis->title . "』中的回答被『管理员』修改过,
				<a href='/q/" . Utils::id2url($dis->id) . "/a/" . Utils::id2url($com->id) . "' target='_blank'>点击查看</a>";

                Message::create($data);
            }
            return Redirect::to("/q/" . Utils::id2url($com->taskid));
        } else {
            $id = Utils::url2id($id);
            return View::make("dis.editor")->with(compact("dis"))->with(compact("com"));
        }


    }

    public function create(Request $request, $id = 0)
    {
        $input = $request->all();
        if ($id == 0) {
            $data = array();
            if ($input['filter'] != "任务") $data['tag'] = $input['filter'];
            else $data['tag'] = $input['tag'];
            $data['title'] = $input['title'];
            $data['content'] = $input['text'];
            $data['userid'] = Auth::user()->id;
            $data['rtime'] = time();
            $d = Discuss::create($data);
            return Redirect::to("/q/" . Utils::id2url($d->id));
        } else {
            $dis = Discuss::find($id);
            if (Auth::user()->id != $dis->userid && !Permission::user_is_super())
                return Redirect::to("/discuss/all");
            if ($input['filter'] != "任务") $dis->tag = $input['filter'];
            else $dis->tag = $input['tag'];
            $dis->title = $input['title'];
            $dis->content = $input['text'];
            $dis->save();
        }
        return Redirect::to("/q/" . Utils::id2url($id));
    }

    public function reply(Request $request)
    {
        $input = $request->all();
        $data = array();
        $data['type'] = 1;
        $data['userid'] = Auth::user()->id;
        $data['comment'] = $input['text'];
        $data['comment_html'] = $input['text_html'];
        $data['taskid'] = $input['cid'];
        $c = Comment::create($data);
//        Mail::send('emails.newmsg', ['msg' => "有用户在讨论区写了新的回答"], function ($message) {
//            $message->to('oreo@qlcoder.com', 'John Smith')->subject('Welcome!');
//        });

        $dis = Discuss::find($input['cid']);
        $dis->rtime = time();
        $dis->save();
        $u = User::find($dis->userid);
        $u->msgcnt += 1;
        $u->save();
        $data = array();
        $data['senderid'] = 1;
        $data['receiverid'] = $u->id;
        $data['content'] = Auth::user()->name . "回答了你的问题:『" . $dis->title . "』<a target='_blank' href='/q/" . Utils::id2url($input['cid']) . "'>点击查看</a>";
        $now = Message::create($data);
        return Redirect::to("/q/" . Utils::id2url($input['cid']) . "#" . Utils::id2url($c->id));

    }
}
